﻿using XFControls.Utils;
using XFControls.XFPanels.XFPanelItems;
using System.Drawing;
// ReSharper disable InconsistentNaming

namespace XFSenseUI.XFItems
{
    public class SenseItemBase : XFItemBase
    {
        protected Rectangle _mRec;
        protected Rectangle _sRec;
        protected readonly int _iconSize = 50;
        protected const int _textSpace = 3;

        public string Icon { get; set; }
        public string MainText { get; set; }
        public string SecondaryText { get; set; }

        public bool MultiLine { get; set; }

        public SenseItemBase()
        {
            if (XFGraphics.GetScreenResolutionNotRotated() == ScreenResolution.QVGA)
                _iconSize = 25;
        }

        protected int CalcRecs(int rightPadding)
        {
            int baseSize = _iconSize + Style.PaddingTop + Style.PaddingBottom;
            int height = Style.PaddingTop;
            int width = GetWidth() - rightPadding;
            int currX = Style.PaddingLeft + rightPadding;
            if (!string.IsNullOrEmpty(Icon))
                currX += _iconSize + Style.PaddingLeft;

            if (!MultiLine)
            {
                MainText = XFControlUtils.EllipsisWord(width - (currX + Style.PaddingRight), MainText, Style.TextFont);

                if (!string.IsNullOrEmpty(SecondaryText))
                    SecondaryText = XFControlUtils.EllipsisWord(width - (currX + Style.PaddingRight), SecondaryText, Style.SecondaryTextFont);
            }

            if (!string.IsNullOrEmpty(MainText))
            {
                _mRec = new Rectangle(0, 0, width - (currX + Style.PaddingRight), 0);
                var mes = Style.TextFont.MeasureString(MainText, _mRec);

                _mRec.X = currX;
                _mRec.Height = mes.Height;
                _mRec.Y = height;

                height += mes.Height;
            }
            if (!string.IsNullOrEmpty(SecondaryText))
            {
                _sRec = new Rectangle(rightPadding, 0, width - (currX + Style.PaddingRight), 0);
                var mes = Style.SecondaryTextFont.MeasureString(SecondaryText, _sRec);

                _sRec.X = currX;
                _sRec.Height = mes.Height;
                _sRec.Y = height + _textSpace;

                height += mes.Height + _textSpace;
            }
            height += Style.PaddingBottom;

            return height < baseSize ? baseSize : height;
        }
    }
    public class SenseControl : SenseItemBase { public SenseItemBase Owner { get; set; } }

    public class SenseItem : SenseItemBase
    {
        public XFItemClickResult ClickResults = XFItemClickResult.Nothing;

        private int _height = -1;
        public SenseItem()
        {
            ItemType = XFItemType.Clickable;
            SetupStyle();
        }

        private void SetupStyle()
        {
            Style = new XFItemStyle
                    {
                        BorderBottomColor = Color.FromArgb(189, 182, 189),
                        DashStyleBottom = System.Drawing.Drawing2D.DashStyle.Dash,
                        TextColor = Color.Black,
                        TextFont = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular),
                        SelectedTextFont = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular),
                        SecondaryTextFont = new Font(FontFamily.GenericSansSerif, 7, FontStyle.Regular),
                        SelectedSecondaryTextFont = new Font(FontFamily.GenericSansSerif, 7, FontStyle.Regular),
                        SelectedBackgroundImageString = "item-select-background",
                        SecondaryTextColor = Color.FromArgb(57, 52, 57),
                        SelectedTextColor = Color.White,
                        SelectedBackgroundColor = Color.FromArgb(43, 36, 43),
                        SelectedSecondaryTextColor = Color.FromArgb(182, 178, 182),
                        Padding = 11,
                        PaddingBottom = 12,
                        PaddingLeft = 10,
                        PaddingRight = 16
                    };
        }

        public override XFItemClickResult GetClickResult()
        {
            OnClick();
            return ClickResults;
        }

        public override int GetHeight()
        {
            if (!Visible)
                return 0;

            if (_height > 0)
                return _height;

            _height = CalcRecs(0);

            return _height;
        }

        public override void ItemPaint(Graphics g, int x, int y)
        {
            if (Style == null)
                SetupStyle();

            base.ItemPaint(g, x, y);

            Font fontMain = getCurrentFont(false);
            Font fontSecondary = getCurrentFont(true);
            Color colorMain = getCurrentFontColor(false);
            Color colorSecondary = getCurrentFontColor(true);

            var mRec = _mRec;
            mRec.Y += y;

            var sRec = _sRec;
            sRec.Y += y;

            if (!string.IsNullOrEmpty(MainText))
                g.DrawString(MainText, mRec, Style, fontMain, colorMain);
            if (!string.IsNullOrEmpty(SecondaryText))
                g.DrawString(SecondaryText, sRec, Style, fontSecondary, colorSecondary);

            if (!string.IsNullOrEmpty(Icon) && Style != null)
                DrawUI(Icon, g, new Rectangle(Style.PaddingLeft, Style.PaddingTop + y, _iconSize, _iconSize));
        }

        public override string ToString()
        {
            if (!string.IsNullOrEmpty(MainText))
                return MainText;
            return "";
        }
    }
}
// ReSharper restore InconsistentNaming
